﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Net;


namespace Mighty.Brain
{
    public class MightyModule
    {
        public Hashtable moduleHash = new Hashtable();
        
        public void LoadDLL()
        {
            try
            {
                // init
                moduleHash.Clear();

                // dll
                String searchPattern = "*.dll";
                SearchOption searchOption = SearchOption.AllDirectories;
                string[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory(), searchPattern, searchOption);
                foreach (String filePath in fileEntries)
                {
                    if (filePath.Contains("DirectX")) continue;

                    try
                    {
                        String fileName = Path.GetFileNameWithoutExtension(filePath);
                        AssemblyName assemblyName = new AssemblyName(fileName);
                        Assembly sampleAssembly = Assembly.Load(assemblyName);
                        Type[] types = sampleAssembly.GetTypes();
            
                        foreach (Type type in types)
                        {
                            String name = type.Name;
                            String space = type.Namespace;
                            String moduleID = String.Format("{0}.{1}", fileName, name);

                            System.Diagnostics.FileVersionInfo fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
                            String comments = fileVersionInfo.Comments;
                            String fileVersion = fileVersionInfo.FileVersion;
                            String fileDescription = fileVersionInfo.FileDescription;
                            String dllNote = String.Format("{0} / {1} / {2} ", fileDescription, fileVersion, comments);

                            // Namespace가 "Mighty.Brain"에 속하지 않으면 load하지 않는다
                            // Class이름에 "Brain"이 들어 있지 않으면 load하지 않는다
                            if (!space.Contains("Mighty.Brain")) continue;
                            if (!name.Contains("Brain")) continue;

                            ModuleInfo m = new ModuleInfo();
                            m.moduleID = moduleID;
                            m.dllName = fileName;
                            m.dllNote = dllNote;
                            m.dllPath = filePath;
                            m.moduleName = name;
                            m.moduleType = type;
                            moduleHash.Add(moduleID, m);
                        }
                    }
                    catch (Exception ex) { }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        public void DownloadUpdatedDLL()
        {
            try
            {
                String searchPattern = "*.dll";
                SearchOption searchOption = SearchOption.AllDirectories;
                String[] fileEntries = Directory.GetFiles(Directory.GetCurrentDirectory(), searchPattern, searchOption);

                String[] listFileName = new String[fileEntries.Length];
                String[] listFileVersion = new String[fileEntries.Length];

                int i = 0;
                foreach (String filePath in fileEntries)
                {
                    String fileName = Path.GetFileName(filePath);
                    String fileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath).FileVersion;

                    listFileName[i] = fileName;
                    listFileVersion[i] = fileVersion;
                    i++;
                }

                //web.Service service = new web.Service();
                //String[] listDownloadURL = service.GetDownloadURL(listFileName, listFileVersion);

                //for (i = 0; i < listDownloadURL.Length; i++)
                //{
                //    String url = listDownloadURL[i];
                //    DownloadDLL(url);
                //}

                //if (listDownloadURL.Length > 0)
                //    MessageBox.Show(listDownloadURL.Length + "개의 최신모듈을 다운로드 받았습니다");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void DownloadDLL(String url)
        {
            try
            {
                String name = Path.GetFileName(url);
                String fullpath = Path.GetFullPath(name);

                WebClient web = new WebClient();
                web.DownloadFile(url, fullpath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }

    public class ModuleInfo
    {
        public String moduleID;
        public String dllName;
        public String dllNote;
        public String dllPath;
        public String moduleName;
        public Type moduleType;
    }
}
